package com.example.pwj.ffstudy.ui.zhihu.model;

import com.example.pwj.ffstudy.bean.zdaily.ZDailyComment;
import com.example.pwj.ffstudy.bean.zdaily.ZDailyCommentSection;
import com.example.pwj.ffstudy.bean.zdaily.ZDailyComments;
import com.example.pwj.ffstudy.http.Api;
import com.example.pwj.ffstudy.ui.zhihu.contract.ZNewsCommContract;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.Single;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

/**
 * Created by pwj on 2017/8/28.
 */

public class ZNewsCommModel implements ZNewsCommContract.Model {
    @Inject
    public ZNewsCommModel() {
    }

    @Override
    public Observable<List<ZDailyComment>> getZDailyLongComment(int id) {
        return Api.getDefault(Api.TYPE_ZHIHU)
                .getZDailyLongComment(id)
                .map(new Function<ZDailyComments, List<ZDailyComment>>() {
                    @Override
                    public List<ZDailyComment> apply(@NonNull ZDailyComments zDailyComments) throws Exception {
                        return zDailyComments.getComments();
                    }
                });
    }

    @Override
    public Observable<List<ZDailyComment>> getZDailyShortComment(int id) {
        return Api.getDefault(Api.TYPE_ZHIHU)
                .getZDailyShortComment(id)
                .map(new Function<ZDailyComments, List<ZDailyComment>>() {
                    @Override
                    public List<ZDailyComment> apply(@NonNull ZDailyComments zDailyComments) throws Exception {
                        return zDailyComments.getComments();
                    }
                });
    }

    public Single<List<ZDailyCommentSection>> convert(final Observable<List<ZDailyComment>> observable,
                                                      String long_header, final String short_header) {
        return observable.concatMap(new Function<List<ZDailyComment>, ObservableSource<ZDailyComment>>() {
            @Override
            public ObservableSource<ZDailyComment> apply(@NonNull List<ZDailyComment> zDailyComments) throws Exception {
                return Observable.fromIterable(zDailyComments);
            }
        }).map(new Function<ZDailyComment, ZDailyCommentSection>() {
            @Override
            public ZDailyCommentSection apply(@NonNull ZDailyComment zDailyComment) throws Exception {
                return new ZDailyCommentSection(zDailyComment);
            }
        })
        .startWith(new ZDailyCommentSection(true, long_header,true))
        .concatWith(new Observable<ZDailyCommentSection>() {
            @Override
            protected void subscribeActual(Observer<? super ZDailyCommentSection> observer) {
                observer.onNext(new ZDailyCommentSection(true, short_header, false));
                observer.onComplete();
            }
        })
        .toList();
    }

    /**
     * page和pagesize用于分页.
     * @param observable
     * @param page
     * @param pageSize
     * @return
     */
    public Single<List<ZDailyCommentSection>> convert(Observable<List<ZDailyComment>> observable,
                                                      int page,int pageSize) {
        return observable.concatMap(new Function<List<ZDailyComment>, ObservableSource<ZDailyComment>>() {
            @Override
            public ObservableSource<ZDailyComment> apply(@NonNull List<ZDailyComment> zDailyComments) throws Exception {
                return Observable.fromIterable(zDailyComments);
            }
        })
        .map(new Function<ZDailyComment, ZDailyCommentSection>() {
            @Override
            public ZDailyCommentSection apply(@NonNull ZDailyComment zDailyComment) throws Exception {
                return new ZDailyCommentSection(zDailyComment);
            }
        })
       /* .take(page*pageSize)
        .takeLast(pageSize)*/
        .toList();
    }
}
